package com.topwalk;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Scanner;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * 压缩文件解压工具类。
 * <p>
 * -----------------------------------------------------------------------------
 * <p>
 * 工程名 ： IWP
 * <p>
 * 授权 : (C) Copyright TopWalk Corporation 2011
 * <p>
 * 公司 : 北京天行网安信息技术有限责任公司
 * <p>
 * -----------------------------------------------------------------------------
 * <p>
 * <font color="#FF0000">注意: 本内容仅限于北京天行网安公司内部使用，禁止转发</font>
 * <p>
 * 
 * @version 1.0
 * @author 张华永
 * @lastModify 2011-11-11
 * @since JDK1.6
 */
public class UnZip {
	
	public static void main(String argv[]) {
		
		Scanner scanner = new Scanner(System.in);
		System.out.print("请输入目的路径：");	//eg:D:/tmp/IWP/
		String outputDirectory = scanner.next();
		System.out.print("请输入源文件路径：");	//eg:D:/IWP.zip
		String src_zip = scanner.next();
		ZipFile zipFile = null;
		try {
			zipFile = new ZipFile(src_zip);
		} catch (IOException e) {
			e.printStackTrace();
		}

		upZip(outputDirectory, zipFile);
	}
	
	/**
	 * 外部调用方法
	 * 
	 * @param outputDirectory 解压到的位置，绝对路径字符串。注意：最后要加/。eg:D:/TEMP/
	 * @param src_zip 压缩文件路径字符串。eg:D:/IWP.zip
	 */
	public static void unZipMain(String outputDirectory,String src_zip){
		ZipFile zipFile = null;
		try {
			zipFile = new ZipFile(src_zip);
		} catch (IOException e) {
			e.printStackTrace();
		}

		upZip(outputDirectory, zipFile);
	}

	/**
	 * 解压文件
	 * 
	 * @param outputDirectory 解压到的位置，绝对路径
	 * @param zipFile 压缩文件路径
	 */
	public static void upZip(String outputDirectory, ZipFile zipFile) {
		try {
			/**
			 * 获取压缩文件的条目枚举
			 */
			Enumeration<?> e = zipFile.entries();

			ZipEntry zipEntry = null;

			createDirectory(outputDirectory, "");
			while (e.hasMoreElements()) {
				zipEntry = (ZipEntry) e.nextElement();
				if (zipEntry.isDirectory()) {
					String name = zipEntry.getName();
					name = name.substring(0, name.length() - 1);
					File f = new File(outputDirectory + File.separator + name);
					f.mkdir();
				} else {
					String fileName = zipEntry.getName();
					fileName = fileName.replace('\\', '/');
					if (fileName.indexOf("/") != -1) {
						createDirectory(outputDirectory, fileName.substring(0,
								fileName.lastIndexOf("/")));
					}

					File f = new File(outputDirectory + zipEntry.getName());

					f.createNewFile();
					InputStream in = zipFile.getInputStream(zipEntry);
					FileOutputStream out = new FileOutputStream(f);

					byte[] by = new byte[1024];
					int c;
					while ((c = in.read(by)) != -1) {
						out.write(by, 0, c);
					}
					out.close();
					in.close();
				}
			}
		} catch (Exception ex) {
			System.out.println(ex.getMessage());
			ex.printStackTrace();
		}
	}

	/**
	 * 创建目录
	 * 
	 * @param directory
	 * @param subDirectory 子目录
	 */
	private static void createDirectory(String directory, String subDirectory) {
		String dir[];
		File fl = new File(directory);
		try {
			if (subDirectory == "" && fl.exists() != true)
				fl.mkdir();
			else if (subDirectory != "") {
				dir = subDirectory.replace('\\', '/').split("/");
				for (int i = 0; i < dir.length; i++) {
					File subFile = new File(directory + File.separator + dir[i]);
					if (subFile.exists() == false)
						subFile.mkdir();
					directory += File.separator + dir[i];
				}
			}
		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		}
	}
}